
package com.weavernorth.humanresource;


import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Calendar;
import java.util.Iterator;


//销售提成推送SAP
public class HRXstcZddpBonusAction implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean bb = new BaseBean();
        SalarRangeUtil salarRangeUtil = new SalarRangeUtil();
        //主表数据库
        RecordSet rs = new RecordSet();
        //提示信息
        String strMessage = "";

        String testType = "";

        boolean isSuccess = false;
        //获取当前日期
        Calendar now = Calendar.getInstance();
        int day = now.get(Calendar.DAY_OF_MONTH);
        String requestid = requestInfo.getRequestid();//请求ID
        String workflowid = requestInfo.getWorkflowid();//流程ID
        String tablename = requestInfo.getRequestManager().getBillTableName();//表单名称
        bb.writeLog("人事-销售提成-终端店铺tablename的值是:" + tablename + "\n requestid" + requestid);
        //主表数据库字段
        String rq = "";//日期
        RecordSet mbrs = new RecordSet();//码表rs
        String mbsql = "";  //查询码表sql
        //店铺编号
        String strDpbh="";
        //1.获取主表单数据
        String sql = " select * from " + tablename + " where requestid =" + requestid;
        bb.writeLog("人事-销售提成-终端店铺接口sql的值是:" + sql + "\n");
        rs.execute(sql);
        if (rs.next()) {
            rq = rs.getString("sqsj");//申请时间
            strDpbh=Util.null2String(rs.getString("dpbh"));
            bb.writeLog("人事-销售提成-终端店铺:日期sxrq的值是:" + rq + "\n");
        }

        //2.封装sap请求xml
        StringBuffer xml = new StringBuffer("");
        xml.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:rfc:functions\">");
        xml.append("<soapenv:Header/>");
        xml.append("<soapenv:Body>");
        xml.append("<urn:ZHRI_077>");
        //Return字段
        xml.append("<RETURN>");
        xml.append("<item>");
        xml.append("<TYPE></TYPE>");
        xml.append("<ID></ID>");
        xml.append("<NUMBER></NUMBER>");
        xml.append("<MESSAGE></MESSAGE>");
        xml.append("<LOG_NO></LOG_NO>");
        xml.append("<LOG_MSG_NO></LOG_MSG_NO>");
        xml.append("<MESSAGE_V1></MESSAGE_V1>");
        xml.append("<MESSAGE_V2></MESSAGE_V2>");
        xml.append("<MESSAGE_V3></MESSAGE_V3>");
        xml.append("<MESSAGE_V4></MESSAGE_V4>");
        xml.append("<PARAMETER></PARAMETER>");
        xml.append("<ROW></ROW>");
        xml.append("<FIELD></FIELD>");
        xml.append("<SYSTEM></SYSTEM>");
        xml.append("</item>");
        xml.append("</RETURN>");
        xml.append("<T_9135>");
        //子表数据
        RecordSet dt1rs = new RecordSet();
        //1.获取子表单dt1数据
        String dt1sql = " select  * from " + tablename + "_dt1 dt1 " + " where dt1.mainid=( select id from " + tablename + " mt where mt.requestid =" + requestid + ")";
        bb.writeLog("人事-销售提成-终端店铺接口dt1sql的值是:" + dt1sql + "\n");
        dt1rs.execute(dt1sql);
        while (dt1rs.next()) {
            //****************销售提成-零售经理***************************

            //获取数据
            String ygbh = dt1rs.getString("ygbh");//员工编号
            //根据员工号查询人员表里是否有,没有则不传此数据
            String strUserid=salarRangeUtil.getUserid(ygbh);
            if("".equals(strUserid)){
                continue;
            }

            //员工姓名
            String strXm=Util.null2String(dt1rs.getString("xm"));
            //zm 增加,判断工资范围是否锁定 开始
            String strGzfw=salarRangeUtil.getGzfwFromUser(strXm);

            String strLockMess = salarRangeUtil.judgeLock(strGzfw, rq);
            bb.writeLog("工资核算范围返回信息:" + strLockMess);
            if (!"".equals(strLockMess)) {
                strLockMess="工号为:"+ygbh+","+strLockMess+"薪资核算结束后，工资范围解除锁定，流程可以继续提交";
                requestInfo.getRequestManager().setMessagecontent(strLockMess);
                return FAILURE_AND_CONTINUE;

            }
            //zm 增加,判断工资范围是否锁定 结束


            double rw = Util.getDoubleValue(dt1rs.getString("yrw"),0.0);//任务-月任务

            double zjyj =  Util.getDoubleValue(dt1rs.getString("yxs"),0.0);//正价业绩-正价销售额
            double zjyj1 =  0.0;//折价业绩-折价销售额

            double wcl = Util.getDoubleValue(dt1rs.getString("wcb"),0.0);//完成率

            double zjjj =  0.0;//正价奖金
            double zjjj1 = 0.0;//折价奖金

            double zjj =  Util.getDoubleValue(dt1rs.getString("zztcjj"),0.0);//总奖金

            String tcrq = rq;//提成日期

            //转码

            //子表数据
            xml.append("<item>");
            xml.append("<PERNR>" + ygbh + "</PERNR>");//员工编号(子表)

            xml.append("<ZRWJE>" + rw + "</ZRWJE>");//任务(子表)

            xml.append("<ZZJJE>" + zjyj + "</ZZJJE>");//正价业绩(子表)


            xml.append("<ZZJJE1>" + zjyj1 + "</ZZJJE1>");//折价业绩(子表)

            xml.append("<ZPER>" + wcl + "</ZPER>");//完成率(子表)


            xml.append("<ZZJJJ>" + zjjj + "</ZZJJJ>");//正价奖金(子表)

            xml.append("<ZZJJJ1>" + zjjj1 + "</ZZJJJ1>");//折价奖金(子表)

            xml.append("<ZJJ>" + zjj + "</ZJJ>");//总奖金(子表)


            xml.append("<ZDATE>" + tcrq + "</ZDATE>");//提成日期(子表)
            //店铺编号
            xml.append("<ZSAPID>"+strDpbh+"</ZSAPID>");

            xml.append("</item>");


        }


        xml.append("</T_9135>");

        xml.append("</urn:ZHRI_077>");

        xml.append("</soapenv:Body>");
        xml.append("</soapenv:Envelope>");


        bb.writeLog("人事-销售提成-终端店铺接口XML的值是:" + xml.toString());


        try {
            //3.推送数据


            String txurl = Util.null2String(bb.getPropValue("personnel-erdos", "xstcurl"));


            String responseXml = salarRangeUtil.sendPost(txurl, xml.toString(), "utf-8");

            bb.writeLog("人事-销售提成-终端店铺接SAP返回的值是:" + responseXml.toString());

            //===========SAP 返回解析====================

            String strWorkcode = "";

            //xmlStr为上图格式的字符串


            String EPernr = "";//员工编码

            Document doc = null;
            try {
                doc = DocumentHelper.parseText(responseXml.toString());
                Iterator<Element> its2 = doc.selectNodes("//TYPE").iterator();
                while (its2.hasNext()) {
                    Element element = its2.next();
                    testType = element.getText();

                    if (!"".equals(testType)) {

                        isSuccess = true;
                        bb.writeLog("推送成功testType值为:" + testType + "\n");


                        break;
                    }


                }

                //返回值失败时,则提示失败
                if ("E".equals(testType)) {
                    Iterator<Element> its3 = doc.selectNodes("//MESSAGE").iterator();
                    while (its3.hasNext()) {
                        Element element2 = its3.next();
                        strMessage = element2.getText();
                        if (!"".equals(strMessage)) {
                            break;
                        }
                    }

                }


            } catch (Exception e) {
                e.printStackTrace();
            }


            //根据返回值是否提交流程
            if ("S".equals(testType)) {
                return SUCCESS;
            } else {
                if (!"".equals(strMessage)) {
                    requestInfo.getRequestManager().setMessagecontent(strMessage);
                }
                return FAILURE_AND_CONTINUE;
            }


        } catch (Exception e) {
            e.printStackTrace();
            return FAILURE_AND_CONTINUE;
        }
    }
}





























